/**
 * 
 */
package org.xelnaga.defiler.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.xelnaga.defiler.context.UserContext;
import org.xelnaga.defiler.web.context.AccountContextSupport;

/**
 * @author qijb
 *
 */
@Service
public class ClickStreamInterceptor implements HandlerInterceptor {
    @Autowired
    private AccountContextSupport accountContextSupport;
    @Autowired
    private ClickstreamFactory clickstreamFactory;
	/**
	 * 
	 */
	public ClickStreamInterceptor() {
		
	}

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
        // Setup AccountContext and Log Context
        accountContextSupport.processAccountContext(request);

        // Give access to the current account context to the view
        // Note: using the modelAndView in the postHandle would not work
        //       in view returned by Spring Web Flow
        request.setAttribute("accountContext", UserContext.getAccountContext());
        
		clickstreamFactory.addRequest(request, response);
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

}
